﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringMatch.MatchAlgorithms
{
    public static class NaiveMatcher
    {
        /// <summary>
        /// Performs a naive match using the given pattern on the text. Returns a list of indexes of each occurrence of the pattern in the text.
        /// </summary>
        /// <returns></returns>
        public static List<int> Match(string pattern, string text)
        {
            List<int> result = new List<int>();

            int textLength = text.Length;
            int patternLength = pattern.Length;
            int patternIndex;
            //This tries to match the pattern to each consecutive set of characters in the text of the same length as the pattern
            //for each character up to the last character with enough characters after it to contain the pattern
            for (int textIndex = 0; textIndex <= textLength - patternLength; textIndex++)
            {
                //Check if it is the start of a pattern match
                patternIndex = 0;
                while (patternIndex < patternLength && text[textIndex + patternIndex] == pattern[patternIndex])
                    patternIndex++;
                if (patternIndex == patternLength)
                    result.Add(textIndex);
            }

            return result;
        }
    }
}
